package com.wanda.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wanda.model.JsonModelGeneral;
import com.wanda.model.User_functionModel;
import com.wanda.model.Wd_UserModel;
import com.wanda.service.User_functionService;

@Controller
@RequestMapping("/user_function")
public class User_functionController {

	@Resource
	private User_functionService user_functionService;

	@RequestMapping(value = "/user_functionall")
	public @ResponseBody JsonModelGeneral user_functionall(String id,HttpServletRequest request) {
		JsonModelGeneral json = new JsonModelGeneral();
		System.out.println(user_functionService.selectByUserId(id).size());
		json.setRows(user_functionService.selectByUserId(id));
		return json;
	}
	
	
	@RequestMapping(value = "/updateuser_function")
	public @ResponseBody void updateuser_function(HttpServletRequest request,HttpServletResponse response) {
		String xx =request.getParameter("ids");
		String str[]=xx.split(",");  
		
		String id= request.getParameter("use");
		System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"+id+"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
		String info="更新成功 需要重新登录";
		if(id!=null){
	    user_functionService.deleteByUserId(id);
		  if(str!=null){
			  for(String st:str){
				  User_functionModel model = new User_functionModel();
				  model.setUser_id(id);
				  model.setWd_function_id(Integer.parseInt(st));
				  user_functionService.addUser_Function(model);
			  }
		  }else{
			  info="失败 权限id为null";
			//  System.out.println(); 
		  }
	   }else{
		  info="失败 用户id为null";
		   //System.out.println();
	   }
		
		 PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 	     // String info ="<script>if(window.confirm('您没有权限')){window.history.back(0);}else{window.history.back(0);}</script>";	
 	     String info1="<script>alert('"+info+"');</script>";
 	     out.write(info1);
	}
	
	
	
	
}
